package handler

import (
	"github.com/gin-gonic/gin"
	ps "lab-service/proto/security"
	pu "lab-service/proto/user"
	"net/http"
)

type User struct {
	UserClient     pu.UserService
	SecurityClient ps.SecurityService
}

func (u *User) Me(ctx *gin.Context) {
	uRsp, err := u.UserClient.One(ctx, &pu.UserRequest{
		Uid: ctx.Value("uid").(int64),
	})
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, err)
		return
	}

	sRsp, err := u.SecurityClient.One(ctx, &ps.SecurityRequest{
		Uid: ctx.Value("uid").(int64),
	})
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusInternalServerError, err)
		return
	}

	ctx.JSON(http.StatusOK, map[string]interface{}{
		"user": uRsp.User,
		"role": sRsp.Role.String(),
	})
}

func (u *User) Page(ctx *gin.Context) {
	form := new(pu.UserRequest)
	if err := ctx.Bind(form); err != nil {
		ctx.AbortWithStatus(http.StatusBadRequest)
		return
	}

	res, err := u.UserClient.Page(ctx, form)
	if err != nil {
		ctx.AbortWithStatusJSON(http.StatusBadRequest, err)
		return
	}
	ctx.JSON(http.StatusOK, res)
}
